﻿//EasyCallback 1.0.0 * http://jhvivanco.blogspot.com/2010/05/easy-callback.html
//Author: Joel J. Vivanco Vasquez * http://jhvivanco.blogspot.com/

//This file is part of EasyCallback.

// EasyCallback is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.

// EasyCallback is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Lesser General Public License for more details.

// You should have received a copy of the GNU Lesser General Public License
// along with EasyCallback.  If not, see <http://www.gnu.org/licenses/>.

using System.Web.UI;
using System;

namespace EasyCallback
{
    public sealed class CallbackEventHandler
        : Control, ICallbackEventHandler
    {
        private readonly Page _currentPage;
        public event Func<string, string> RaiseCallbackEventControl;
        public string Result { set; get; }

        public CallbackEventHandler()
        {
            _currentPage = ((Page)Context.CurrentHandler);
            _currentPage.Load += CurrentPageLoad;
        }

        /// <summary>
        /// Gets the Callback Event Reference: function(arg, onSuccess  ,context, onError)
        /// </summary>
        /// <returns>"function(arg, onSuccess , context, onError)"</returns>
        void CurrentPageLoad(object sender, EventArgs e)
        {
            if (_currentPage != null)
            {
                if (_currentPage.Controls != null) _currentPage.Controls.Add(this);

                var callbackReferenceString = String.Concat(
                    "function(arg, onSuccess , context, onError) { if(typeof ajax_beginRequest == 'function') ajax_beginRequest();",
                    _currentPage.ClientScript.GetCallbackEventReference(
                        this,
                        "arg",
                        String.Concat("function(output, context) {",
                                      "onSuccess(output, context); if(typeof ajax_endRequest == 'function') ajax_endRequest(); }"),
                        "context",
                        String.Concat("function(output, context){",
                                      "onError(output, context); if(typeof ajax_errorRequest == 'function') ajax_errorRequest(); }"),
                        false), "}"
                    );

                _currentPage.ClientScript.RegisterClientScriptBlock(GetType(),
                                                                    "CallbackEventReference_" +
                                                                    ClientID,
                                                                    string.Concat("var ",
                                                                                  RaiseCallbackEventControl.Method.Name,
                                                                                  " = ",
                                                                                  callbackReferenceString,
                                                                                  ";"),
                                                                    true);
            }
        }

        #region ICallbackEventHandler Members

        string ICallbackEventHandler.GetCallbackResult()
        {
            return Result;
        }


        void ICallbackEventHandler.RaiseCallbackEvent(string eventArgument)
        {
            var control = RaiseCallbackEventControl;
            if (control != null) Result = control(eventArgument);
        }

        #endregion
    }
}